Home:ALL Converter>Unix newlines to Windows newlines (on Windows)

Unix newlines to Windows newlines (on Windows)

Ask Time:2009-04-07T11:57:30         Author:Eddie Groves

Json Formatter

Is there a way (say PowerShell, or a tool) in Windows that can recurse over a directory and convert any Unix files to Windows files.

I'd be perfectly happy with a way in PowerShell to at least detect a Unix file.

It's easy do this for one single file, but I'm after something a bit more scalable (hence leaning towards a PowerShellish solution).

Author:Eddie Groves,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/724083/unix-newlines-to-windows-newlines-on-windows
i_am_jorf :

You can use Visual Studio. Menu File → Advanced Save Options....",
2009-04-07T04:35:11
js2010 :

Converting to Windows text could be as simple as:\n(Get-Content file) | Set-Content file\n\nUse the following (with negative lookbehind). Without -nonewline, set-content puts an extra `r`n at the bottom. With the parentheses, you can modify the same file. This should be safe on doing to the same file twice accidentally.\nfunction unix2dos ($infile, $outfile) {\n (Get-Content -raw $infile) -replace "(?<!`r)`n","`r`n" |\n Set-Content -nonewline $outfile\n}\n\nThe reverse would be this, Windows to Unix text:\nfunction dos2unix ($infile, $outfile) {\n (Get-Content -raw $infile) -replace "`r`n","`n" |\n Set-Content -nonewline $outfile\n}\n\nHere's another version for use with huge files that can't fit in memory. But the output file has to be different.\nFunction Dos2Unix ($infile, $outfile) {\n Get-Content $infile -ReadCount 1000 | % { $_ -replace '$',"`n" } |\n Set-Content -NoNewline $outfile\n}\n\nExamples (input and output file can be the same):\ndos2unix dos.txt unix.txt\nunix2dos unix.txt dos.txt\nunix2dos file.txt file.txt\n\nIf you have Emacs, you can check it with esc-x hexl-mode. Notepad won't display Unix text correctly; it will all be on the same line. I have to specify the path for set-content, because -replace erases the pspath property.",
2018-10-01T14:21:57
ninesided :

If Cygwin isn't for you, there are numerous stand-alone executables for unix2dos under Windows if you google around, or you could write one yourself. See my similar (opposite direction for conversion) question here.",
2009-04-07T04:13:04
Arvind :

The result of testing the code given in a previous answer in a loop with 10,000 files, many of them more than 50 KB in size:\nThe bottom line is the PowerShell code is very inefficient/slow/unusable for large files and large number of files. It also does not preserve BOM bytes. I found unix2dos 7.2.3 to be the fastest and most practical solution.",
2015-08-01T01:21:59
Dmitri :

It works for me:\n\n Get-ChildItem -Recurse -File | % { $tmp = Get-Content $_; $tmp | Out-File \"$_\" -Encoding UTF8 }\n",
2018-11-13T01:44:27
GeekyMonkey :

Building on js2010's answer I've created this script:\n$excludeFolders = "node_modules|dist|.vs";\n$excludeFiles = ".*\\.map.*|.*\\.zip|.*\\.png|.*\\.ps1"\n\nFunction Dos2Unix {\n [CmdletBinding()]\n Param([Parameter(ValueFromPipeline)] $fileName)\n\n Write-Host -Nonewline "."\n\n $fileContents = Get-Content -raw $fileName\n $containsCrLf = $fileContents | %{$_ -match "\\r\\n"}\n If($containsCrLf -contains $true)\n {\n Write-Host "`r`nCleaing file: $fileName"\n Set-Content -Nonewline -Encoding utf8 $fileName ($fileContents -replace "`r`n","`n")\n }\n}\n\nGet-Childitem -File "." -Recurse |\n Where-Object {$_.PSParentPath -notmatch $excludeFolders} |\n Where-Object {$_.PSPath -notmatch $excludeFiles} |\n foreach { $_.PSPath | Dos2Unix }\n",
2020-04-22T10:44:31
JasonMArcher :

Here is the pure PowerShell way if you are interested.\nFinding files with at least one Unix line ending (PowerShell v1):\ndir * -inc *.txt | %{ if (gc $_.FullName -delim "`0" | Select-String "[^`r]`n") {$_} }\n\nHere is how you find and covert Unix line endings to Windows line endings. One important thing to note is that an extra line ending (\\r\\n) will be added to the end of the file if there isn't already a line ending at the end. If you really don't want that, I'll post an example of how you can avoid it (it is a bit more complex).\nGet-ChildItem * -Include *.txt | ForEach-Object {\n ## If contains UNIX line endings, replace with Windows line endings\n if (Get-Content $_.FullName -Delimiter "`0" | Select-String "[^`r]`n")\n {\n $content = Get-Content $_.FullName\n $content | Set-Content $_.FullName\n }\n}\n\nThe above works because PowerShell will automatically split the contents on \\n (dropping \\r if they exist) and then add \\r\\n when it writes each thing (in this case a line) to the file. That is why you always end up with a line ending at the end of the file.\nAlso, I wrote the above code so that it only modifies files that it needs to. If you don't care about that you can remove the if statement. Oh, make sure that only files get to the ForEach-Object. Other than that, you can do whatever filtering you want at the start of that pipeline.",
2009-04-07T20:02:40
Miserable Variable :

There is dos2unix and unix2dos in Cygwin.",
2009-04-07T04:05:04
Keith G. :

This seems to work for me.\nGet-Content Unix.txt | Out-File Dos.txt\n",
2016-09-19T14:26:35
soulmerge :

Download Vim, open your file, and issue\n:se fileformat=dos|up\n\nBatch for multiple files (all *.txt files in C:\\tmp - recursive):\n:args C:\\tmp\\**\\*.txt\n:argdo se fileformat=dos|up\n",
2009-04-07T04:31:25
yy